package com.tosmart.jorm.cases;

import com.tosmart.jorm.models.Family;
import org.testng.Assert;
import org.testng.annotations.*;

import java.sql.SQLException;
import java.util.List;

/**
 * @author tosmart@gmail.com
 */
public class BaseMap extends TestBase {

    @BeforeTest
    public void setUp() throws SQLException {
        super.setUp();
    }

    @AfterTest
    public void tearDown() throws SQLException {
        super.tearDown();
    }

    @Test
    public void insert() {

        Family f1 = new Family(1);
        f1.setName("tosmart family");
        f1.setAddress("ChongQing China.");
        f1.setBalance(10000D);

        jormFace.insertEntity(f1);

        Family f2 = new Family(2);
        f2.setName("HeQiang family");
        f2.setAddress("LiaoNing China.");
        f2.setBalance(20000D);

        jormFace.insertEntity(f2);
    }

    @Test(dependsOnMethods = "insert")
    public void load() {

        Family family = jormFace.loadEntity(Family.class, 1);

        Assert.assertNotNull(family);
        Assert.assertEquals(family.getId(), 1);
        Assert.assertEquals(family.getName(), "tosmart family");
        Assert.assertEquals(family.getAddress(), "ChongQing China.");
        Assert.assertEquals(family.getBalance(), 10000D);
    }

    @Test(dependsOnMethods = "load")
    public void list() {

        List<Family> list = jormFace.listEntities(Family.class);

        Assert.assertEquals(list.size(), 2);

        Family f1 = list.get(0);
        Family f2 = list.get(1);

        Assert.assertEquals(f1.getId(), 1);
        Assert.assertEquals(f2.getId(), 2);

        Assert.assertEquals(f1.getName(), "tosmart family");
        Assert.assertEquals(f2.getName(), "HeQiang family");

        Assert.assertEquals(f1.getAddress(), "ChongQing China.");
        Assert.assertEquals(f2.getAddress(), "LiaoNing China.");

        Assert.assertEquals(f1.getBalance(), 10000D);
        Assert.assertEquals(f2.getBalance(), 20000D);
    }

    @Test(dependsOnMethods = "list")
    public void update() {

        Family family = jormFace.loadEntity(Family.class, 1);
        family.setName("new name");

        jormFace.updateEntity(family);

        family = jormFace.loadEntity(Family.class, 1);

        Assert.assertNotNull(family);
        Assert.assertEquals(family.getId(), 1);
        Assert.assertEquals(family.getName(), "new name");
        Assert.assertEquals(family.getAddress(), "ChongQing China.");
        Assert.assertEquals(family.getBalance(), 10000D);
    }

    @Test(dependsOnMethods = "update")
    public void delete() {
        jormFace.deleteEntity(new Family(1));
        Assert.assertNull(jormFace.loadEntity(Family.class, 1));
    }
}
